package cn.chuxing;

import org.hibernate.validator.HibernateValidator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import java.util.Locale;

@SpringBootApplication
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
@ImportResource(locations = {"config/dubbo-consumer.xml"})
public class ManageApplication {

	public static void main(String[] args) {
		SpringApplication.run(ManageApplication.class, args);
	}

	@Bean
	public ResourceBundleMessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasenames("messages/login");
		messageSource.setDefaultEncoding("UTF-8");

		messageSource.setUseCodeAsDefaultMessage(false);
		messageSource.setCacheSeconds(60);
		return messageSource;
	}
	@Bean
	public CookieLocaleResolver localeResolver(){
		CookieLocaleResolver locale = new CookieLocaleResolver();
		locale.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
		return locale;
	}

	@Bean
	public Validator localValidatorFactoryBean() {

		LocalValidatorFactoryBean validator = new  LocalValidatorFactoryBean();
		validator.setValidationMessageSource(messageSource());
		validator.setProviderClass(HibernateValidator.class);

		return validator;
	}

	@Bean(name = "multipartResolver")
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setDefaultEncoding("UTF-8");
		resolver.setMaxInMemorySize(20480);
		resolver.setMaxUploadSize(50 * 1024 * 1024);
		resolver.setResolveLazily(true);
		return resolver;
	}
}
